package com.zw.goods.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zw.commons.utlis.JwtHelper;
import com.zw.goods.service.GoodInfoService;
import com.zw.object.dto.GoodDto;
import com.zw.object.goods.GoodInfo;
import com.zw.object.goods.GoodStock;
import com.zw.goods.mapper.GoodStockMapper;
import com.zw.goods.service.GoodStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.object.goods.GoodType;
import com.zw.object.result.Result;
import com.zw.object.vo.GoodStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

/**
 * <p>
 * 物资出入库表 服务实现类
 * </p>
 *
 * @author zw
 * @since 2023-03-15
 */
@Service
public class GoodStockServiceImpl extends ServiceImpl<GoodStockMapper, GoodStock> implements GoodStockService {
    @Autowired
    private GoodInfoService goodInfoService;
    @Override
    public Result list(GoodStockVo vo) {
        LambdaQueryWrapper<GoodStock> wrapper = new LambdaQueryWrapper<>();
        Page<GoodStock> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        wrapper.eq(vo.getOperateType() != null, GoodStock::getOperateType, vo.getOperateType());
        if (StrUtil.isNotBlank(vo.getStart()) && StrUtil.isNotBlank(vo.getEnd())){
            DateTime a = DateUtil.parse(vo.getStart() + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
            DateTime b = DateUtil.parse(vo.getEnd() + " 23:59:59", "yyyy-MM-dd HH:mm:ss");
            wrapper.between(GoodStock::getCreateTime, a, b);
        }
        wrapper.orderByDesc(GoodStock::getCreateTime);
        Page<GoodStock> stockPage = baseMapper.selectPage(page, wrapper);
        return Result.ok(stockPage);
    }

    @Override
    public Result save(GoodStock goodStock, HttpServletRequest request) {
        List<GoodDto> list = goodStock.getList();
        List<GoodStock> goodStockList = new ArrayList<>();
        list.forEach(goodDto -> {
            GoodStock stock = new GoodStock();
            stock.setCreateBy(JwtHelper.getUsername(request.getHeader("token")));
            stock.setAccept(goodStock.getAccept());
            stock.setGoodName(goodDto.getGoodName());
            stock.setGoodNum(goodDto.getGoodNum());
            stock.setGoodSize(goodDto.getGoodSize());
            stock.setPeopleName(goodStock.getPeopleName());
            stock.setPeoplePhone(goodStock.getPeoplePhone());
            stock.setOperateType(goodStock.getOperateType());
            stock.setRemark(goodStock.getRemark());
            goodStockList.add(stock);
            GoodInfo goodInfo = goodInfoService.getById(goodDto.getId());
            if (null == goodInfo.getTotal()){
                goodInfo.setTotal(0);
            }
            int res;
            if (goodStock.getOperateType() == 0) {
                res = goodInfo.getTotal() + goodDto.getGoodNum();
            } else {
                res = goodInfo.getTotal() - goodDto.getGoodNum();
                if (res < 0){
                    throw new RuntimeException("库存不够");
                }
            }
            goodInfo.setTotal(res);
            goodInfoService.updateById(goodInfo);
        });
        int insert = 0;
        for (GoodStock stock : goodStockList) {
            insert = baseMapper.insert(stock);
        }
        return insert > 0 ? Result.ok():Result.fail();
    }
}
